package com.rran.study.chat.ops.config;

import com.rran.study.chat.ops.netty.server.WebSocketServer;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author yy
 * @Type NettybootServerInitConfig.java
 * @Desc
 * @date 2020/8/6 20:16
 */
@Component
public class NettybootServerInitConfig implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        WebSocketServer webSocketServer = applicationContext.getBean(WebSocketServer.class);
        //防止父类重复监听事件
        if (event.getApplicationContext().equals(applicationContext)) {
            webSocketServer.setPort(8575);
            webSocketServer.start();
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}